package com.example.ovulationperiodtracker.view;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.example.ovulationperiodtracker.R;
import com.example.ovulationperiodtracker.databinding.DialogJingStartBinding;

import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;


public class JingStartEndDialog extends Dialog {

    private OnClickYesNoListener onClickYesNoListener;
    private Context context;
    private String title;

    public JingStartEndDialog(@NonNull Context context,String title) {
        super(context);
        this.context = context;
        this.title = title;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        DialogJingStartBinding binding = DialogJingStartBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        Window window = getWindow();
        WindowManager.LayoutParams layoutParams = window.getAttributes();

        // 获取屏幕宽度
        int screenWidth = getContext().getResources().getDisplayMetrics().widthPixels;
        // 设置 Dialog 宽度为屏幕宽度的 80%
        layoutParams.width = (int) (screenWidth * 0.9);
        window.setAttributes(layoutParams);
        window.setBackgroundDrawableResource(R.drawable.bg_dialog_ys);

        setCanceledOnTouchOutside(false);
        setCancelable(false);


        binding.tvNo.setOnClickListener(v -> {
//            if (onClickYesNoListener != null) {
//                onClickYesNoListener.onClickNo();
//            }
            dismiss();
        });

        binding.title.setText(title);

        binding.tvYes.setOnClickListener(v -> {
            if (onClickYesNoListener != null) {


                Date date = binding.dateStartMestruo.getDate();

                LocalDate localDate = date.toInstant()
                        .atZone(ZoneId.systemDefault())
                        .toLocalDate();

                if (localDate.isAfter(LocalDate.now())) {
                    Toast.makeText(context, "不能在未来的时间添加记录", Toast.LENGTH_SHORT).show();
                    return;
                }
                onClickYesNoListener.onClickYes(localDate);
                dismiss();
            }

        });
    }

    public JingStartEndDialog setOnClickYesNoListener(OnClickYesNoListener onClickYesNoListener) {
        this.onClickYesNoListener = onClickYesNoListener;
        return this;
    }

    public interface OnClickYesNoListener {
        void onClickYes(LocalDate localDate);

        // void onClickNo();
    }
}